<script setup lang="ts">
import type { Status } from "@/types/Status";
import axios from "axios";
//用于控制弹框的显示和隐藏
const visible = ref(false);
//用于存储城市选择组件的最外层元素
const target = ref<HTMLDivElement>();
//用于缓存城市数据
let cachedCities: City[] = [];
//用于保存城市数据的加载状态
const status = ref<Status>("idle");
//控制弹框显示
async function show() {
  //显示弹层
  visible.value = true;
  // 如果城市数据不存在
  if (cachedCities.length === 0) {
    // 更新城市数据加载状态
    status.value = "loading";
    // 发送请求获取城市数据
    const response = await axios.get(
      "https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json"
    );

    // 缓存城市数据
    cachedCities = response.data;
  }

  // 更新城市数据加载状态
  status.value = "success";
  //更新城市状态
  cities.value = cachedCities;
}
//控制弹框隐藏
function hide() {
  visible.value = false;
}
//控制弹框切换
function toggle() {
  visible.value ? hide() : show();
}
//在城市选择组件外部点击时隐藏该组件
onClickOutside(target, hide);

// 声明城市数据类型
interface City {
  code: string;
  level: number;
  name: string;
  areaList: City[];
}
//声明城市数据
const cities = ref<City[]>([]);

interface AddressCode {
  provinceCode: string;
  cityCode: string;
  countyCode: string;
}
interface AddressName {
  provinceName: string;
  cityName: string;
  countyName: string;
}
//用于存储省市区编码
const addressCode: Partial<AddressCode> = {};
//用于存储省市区名称
const addressName: Partial<AddressName> = {};
//声明当前组件要触发的自定义事件
const emit = defineEmits<{
  (e: "onChanged", addressCode: AddressCode): void;
  (e: "update:fullLocation"): void;
}>();

// fullLocation: 用户选择的省市区名称
const props = defineProps<{ fullLocation?: string }>();

// 在页面中展示用户的选择结果(城市名称)
const selectedCity = useVModel(props, "fullLocation");

// 更新城市弹层内容, 记录用户选择的城市编码
function updateCities(code: string, level: number, name: string) {
  //更新城市信息
  cities.value = cities.value.find((city) => city.code === code)?.areaList!;
  //匹配城市级别
  switch (level) {
    //省
    case 0:
      //存储省编码
      addressCode.provinceCode = code;
      addressName.provinceName = name;
      break;
    //市
    case 1:
      //存储市编码
      addressCode.cityCode = code;
      addressName.cityName = name;
      break;
    //县、区
    case 2:
      //存储县、区编码
      addressCode.countyCode = code;
      addressName.countyName = name;

      //通过自定义事件的方式将用户选择的省市区编码传递到组件外部
      // emit("onChanged", <Required<AddressCode>>addressCode);
      emit("onChanged", addressCode as Required<AddressCode>);

      //隐藏弹框
      hide();
      //存储用户选择的城市名称
      selectedCity.value =
        addressName.provinceName! +
        addressName.cityName! +
        addressName.countyName!;
  }
  if (level === 2) {
    hide();
  }
}
</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{ active: visible }">
      <span class="placeholder" v-if="!selectedCity">请选择收货地址</span>
      <span class="value" v-else>{{ selectedCity }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="visible">
      <template v-if="status === 'success'">
        <span
          @click="updateCities(city.code, city.level, city.name)"
          class="ellipsis"
          v-for="city in cities"
          :key="city.code"
          >{{ city.name }}</span
        >
      </template>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
